package com.white.commonlib;

import android.content.Context;
import android.text.TextUtils;

import com.white.commonlib.crash.CrashHandler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 应用程序配置类
 * 用于保存当前系统相关信息及设置
 */
public class AppConfig
{
    private String appConfigPath = "config";

    private Context mContext;

    /***网络是否可用*/
    private boolean isNetworkAvilable = false;
    /***网络类型*/
    private String netType = "无状态";

    /**app 文件夹名称*/
    private String appDirName;

    /**log上传地址*/
    private String uploadAddress;

    /**是否处理全局异常*/
    private boolean catchException;

    /**异常出现时，会回调*/
    private CrashHandler.OnShutdownListener shutdownListener;

    public AppConfig(Context mContext)
    {
        this.mContext = mContext;
    }

    public AppConfig setCrashUploadUrl(String uploadAddress)
    {
        this.uploadAddress = uploadAddress;
        return this;
    }

    public String getUploadAddress()
    {
        return uploadAddress;
    }

    public boolean isNetworkAvilable()
    {
        return isNetworkAvilable;
    }

    public AppConfig setNetworkAvilable(boolean networkAvilable)
    {
        isNetworkAvilable = networkAvilable;
        return this;
    }

    public String getNetType()
    {
        return netType;
    }

    public AppConfig setNetType(String netType)
    {
        this.netType = netType;
        return this;
    }

    public boolean isCatchException()
    {
        return catchException;
    }

    public AppConfig enableCatchException(boolean catchException)
    {
        this.catchException = catchException;
        return this;
    }

    public AppConfig setAppConfigPath(String appConfigPath)
    {
        this.appConfigPath = appConfigPath;
        return this;
    }

    public AppConfig setShutdownListener(CrashHandler.OnShutdownListener shutdownListener)
    {
        this.shutdownListener = shutdownListener;
        return this;
    }

    public String getAppConfigPath()
    {
        return appConfigPath;
    }

    public CrashHandler.OnShutdownListener getShutdownListener()
    {
        return shutdownListener;
    }

    public String getAppDirName()
    {
        if(appDirName == null)
        {
            appDirName = mContext.getPackageName();
            appDirName = appDirName.substring(appDirName.lastIndexOf(".") + 1, appDirName.length());
        }
        return appDirName;
    }

    public String get(String key)
    {
        Properties props = get();
        return (props != null) ? props.getProperty(key) : null;
    }

    public Properties get()
    {
        FileInputStream fis = null;
        Properties props = new Properties();
        try
        {
            // 读取app_config目录下的config
            File dirConf = mContext.getDir(appConfigPath, Context.MODE_PRIVATE);
            File config = new File(dirConf, appConfigPath);
            if(!config.exists())
            {
                boolean t = config.createNewFile();
                if(!t)
                    throw new RuntimeException("create config file error!");
            }

            fis = new FileInputStream(dirConf.getPath() + File.separator
                    + appConfigPath);

            props.load(fis);
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                fis.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return props;
    }

    private void setProps(Properties p)
    {
        FileOutputStream fos = null;
        try
        {
            // 把config建在(自定义)app_config的目录下
            File dirConf = mContext.getDir(appConfigPath, Context.MODE_PRIVATE);
            File conf = new File(dirConf, appConfigPath);
            fos = new FileOutputStream(conf);

            p.store(fos, null);
            fos.flush();
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                fos.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    public void set(String key, String value)
    {
        Properties props = get();
        props.setProperty(key, value);
        setProps(props);
    }

    public void remove(String... key)
    {
        Properties props = get();
        for (String k : key)
            props.remove(k);
        setProps(props);
    }

}
